<script setup lang="ts">
import { ref,onBeforeMount } from "vue";
import { useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
import { querySupplierInquiryList,querySupplierInquiryById } from "@/api/supplierInquiry.js"

import path from "node:path";
//创建路由
const router = useRouter();

//查询条件
const supplierInquiry = ref({
  company: "",
  mobile: "",
  status: null
});

//分页数据
const page = ref(1); //当前页 默认第1页
const pageSize = ref(10); //每页记录数
const total = ref(0); //总记录数

//定义表单查询条件
const productInquiryData = ref({});

//表数据
const tableData = ref([{}]);

//每页记录数变化
function chgsize(size){
  //当前页多少页
  (pageSize.value = size);
  //从每页开始查询
  page.value = 1;
  queryInquiry();
}

//page变化
function chgpg(size) {
  (page.value = size);
  queryInquiry()
}

//清空查询条件
function reset() {
  (supplierInquiry.value.company = ""),
  (supplierInquiry.value.mobile= ""),
  (supplierInquiry.value.status= null),
    queryInquiry();
}

//查询产品询价单信息
async function queryInquiry() {
  let data = {
    page: page.value,
    pageSize: pageSize.value,
    company: supplierInquiry.value.company,
    mobile: supplierInquiry.value.mobile,
    status: supplierInquiry.value.status,
  };

  //发送请求
  let res = await querySupplierInquiryList(data);
  if(res.code == 200){
    //把返回的页数和数据边化
    tableData.value = res.data.data;

    //总记录数
    total.value = res.data.total;
    //每页多少数据
    pageSize.value = res.data.pageSize;
    page.value = res.data.page;
  }
}

//编辑报价
async function edit(id) {

  //跳转到编辑页
  router.push({ path: "/product/inquiry/edit", query: { id } })
}

//添加跳转
function add() {
  router.push("/product/inquiry/add");
}


onBeforeMount(()=>{
  queryInquiry();
})
</script>

<template>
  <div>
    <div>
      <div>产品询价</div>
      <el-form>
        <el-row>
          <el-col :span="6">
            <el-form-item label="询价客户">
              <el-input
                v-model="supplierInquiry.company"
                placeholder="请输入"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="联系方式">
              <el-input v-model="supplierInquiry.mobile" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="状态">
              <el-select
                v-model="supplierInquiry.status"
                style="width: 150px"
                placeholder="全部"
              >
                <el-option :value="''" label="全部" />
                <el-option :value="1" label="代报价" />
                <el-option :value="2" label="报价中" />
                <el-option :value="3" label="已报价" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-button @click="queryInquiry">查询</el-button>
          </el-col>
          <el-col :span="2">
            <el-button @click="reset">重置</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div style="float: right; margin-right: 20px; margin-bottom: 20px">
      <el-button @click="add">添加</el-button>
    </div>
    <el-table :data="tableData">
      <el-table-column label="询价编号" prop="code" />
      <el-table-column label="询价客户" prop="company" />
      <el-table-column label="联系人" prop="contactName" />
      <el-table-column label="联系方式" prop="mobile" />
      <el-table-column label="询价时间" prop="createTime" />
      <el-table-column label="销售" />
      <el-table-column label="状态">
        <template v-slot="{ row }">
          <span v-if="row.status == 1">待报价</span>
          <span v-if="row.status == 2">报价中</span>
          <span v-if="row.status == 3">已报价</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-link style="text-decoration: none" @click="edit(row.id)"
            >编辑</el-link
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      background
      :page-size="pageSize"
      layout="total,sizes,prev, pager, next,jumper"
      :total="total"
      @size-change="chgsize"
      @current-change="chgpg"
    />
  </div>
</template>

<style scoped lang="scss"></style>
